package com.hoyin.lightctrl_esp8266;

import android.app.Activity;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class SocketReceiveThread extends Thread {
	private Socket mSocket;
	private Activity mActivity;
	private InputStream inputStream;
	public SocketReceiveThread(Activity activity, Socket socket){
		mSocket = socket;
		mActivity = activity;
	}
	public void run()//重写run方法
	{
		try
		{
			while (true)
			{
				final byte[] buffer = new byte[1024];//创建接收缓冲区
				inputStream = mSocket.getInputStream();
				final int len = inputStream.read(buffer);//数据读出来，并且返回数据的长度
				mActivity.runOnUiThread(new Runnable()//不允许其他线程直接操作组件，用提供的此方法可以
				{
					public void run()
					{
// TODO Auto-generated method stub
						Log.i("HHH", new String(buffer,0,len));
					}
				});
			}
		}
		catch (IOException e)
		{
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
